home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-01-18 | 4.0 KB | 118 lines | [TEXT/MPS ] |
- {
-
- ParseFullPathname
-
- Takes a full pathname and turns it into a DirID and VRefNum pair.
-
- This does not handle partial pathnames because it assumes that the first
- element in the pathname is the volume name.
-
- }
-
- Program ParsePath;
-
- uses
- Files,Memory,Errors,IntEnv;
-
- const
- noError = 0;
-
- type
- longString = record
- length : integer;
- text : Ptr;
- end;
-
- var
- theError : OSErr;
- here : FSSpec;
- theInputString : longString;
- tempByte : Byte;
-
-
-
- function GetElement(theElement: StringPtr; thePathname: longString; elementIndex: integer): boolean;
- var
- elemStart, elemEnd : longint;
- begin
- elemStart := longint(thePathname.text);
- while (elementIndex > 1) and (elemStart <
- longint(thePathname.text) + thePathname.length) do begin { Loop to find start of desired element }
- if Ptr(elemStart)^ = ord(':') then { ":" means we found an element delimiter }
- elementIndex := elementIndex - 1; { If so, elementIndex - 1 elements to go }
- elemStart := elemStart + 1; { Increment for next character in string }
- end;
- elemEnd := elemStart; { Begin looking at start of desired element }
- while (Ptr(elemEnd)^ <> ord(':')) and (elemEnd <
- longint(thePathname.text) + thePathname.length) do { Loop to find ":" at end of desired element }
- elemEnd := elemEnd + 1;
- if (elemEnd > elemStart) and (elemEnd < elemStart + 32) then begin { If the above looped at least once, then }
- BlockMove(Ptr(elemStart),Ptr(longint(theElement) + 1),elemEnd - elemStart); { copy the string element into theElement }
- theElement^[0] := char(elemEnd - elemStart); { Copy in the length of the string }
- GetElement := true;
- end else { If the element's length is 0, routine fails }
- GetElement := false;
- end;
-
-
- function ParseFullPathName(VAR theResult: FSSpec; thePathname: longString) : OSErr; { Walk down thePath, returning an FSSpec }
-
- var
- theOSError : OSErr;
- theLevel : integer;
- theDInfo : CInfoPBRec;
- theVInfo : HParamBlockRec;
-
- begin
- theLevel := 2;
- ParseFullPathName := noError; { And remains so unless an error occurs in here }
-
- theResult.parID := fsRtDirID; { Start at the root directory }
-
- if GetElement(StringPtr(@theResult.name), thePathname, 1) then begin { If there's a valid volume name, }
- with theVInfo do begin
- ioVRefNum := 0; { get its vRefNum… }
- ioNamePtr := Pointer(@theResult.name);
- ioVolIndex := -1;
- end;
- theError := PBHGetVInfo(@theVInfo,false);
- if theError <> 0 then
- ParseFullPathName := theError { and return an error if path not found }
- else
- theResult.vRefNum := theVInfo.ioVRefNum; { Otherwise, save the valid vRefNum }
- end;
-
- while GetElement(StringPtr(@theResult.name), thePathname, theLevel) do begin { Now, walk down the path, }
- with theDInfo do begin
- ioFDirIndex := 0; { and get its DirID }
- ioDrDirID := theResult.parID;
- ioVRefNum := theResult.vRefNum;
- ioNamePtr := Pointer(@theResult.name);
- end;
- theError := PBGetCatInfo(@theDInfo, false);
- if theError <> 0 then
- ParseFullPathName := theError { Return an error if path not found }
- else
- theResult.parID := theDInfo.ioDrDirID; { Otherwise, save the valid path info }
- theLevel := theLevel + 1; { Increment looping variable to go deeper }
- end; { This loop repeats to the bottom of the path }
- end;
-
-
-
- begin { main program }
- if argC <> 2 then writeln('Form: ParseFullPathname <pathname>') { Educate the user, if necessary }
- else begin
- tempByte := Ptr(argV^[1])^;
- theInputString.length := tempByte;
- theInputString.text := Ptr(longint(argV^[1])+1);
- theError := ParseFullPathName(here,theInputString); { Call ParsePath on full pathname string }
- if theError <> noError then
- writeln('Failed on error ',theError,'.') { Return the error from PBHGetVInfo or PBGetCatInfo }
- else begin
- writeln(' vRefNum: ',here.vRefNum); { or write out the results }
- writeln(' DirID: ',here.parID);
- writeln(' Name: ',here.name);
- end;
- end;
- end. { main program }